package com.example.demo1.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.demo1.R
import com.example.demo1.reposity.ArticleItem
import com.example.utils.loadImage
/**
 * 文章列表适配器
 * **/
class ArticleListAdapter(var context: Context, var list: List<ArticleItem>) :
    RecyclerView.Adapter<ArticleListAdapter.MyArtitleViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyArtitleViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.article_item, parent, false)
        return MyArtitleViewHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size ?: 0
    }

    override fun onBindViewHolder(holder: MyArtitleViewHolder, position: Int) {
        val arrayItem = list[position]
        holder.article_title.text = arrayItem.title.toString()
        holder.article_type.text = arrayItem.type.toString()
        holder.article_word.text = arrayItem.wordNum.toString()
        holder.article_lexile.text = arrayItem.lexile.toString()
        loadImage(context, holder.article_img, arrayItem.cover)
        when (arrayItem.type) {
            "人工智能" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_44d7b6))
            "前沿技术" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_01aaa2))
            "太空宇宙" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_7c7ee4))
            "生物医疗" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_32c5ff))
            "自然科学" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_6dd400))
            "环境生态" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_ff9500))
            "历史文化" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_d4b07e))
            "艺术文学" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_f56178))
            "休闲生活" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_7bd2d8))
            "社会现象" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_cc95d9))
            "成长教育" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_3dbc80))
            "心理情感" -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_dd4abc))
            else -> holder.article_type.setBackgroundColor(ContextCompat.getColor(context, R.color.color_9696a0))
        }
    }

    class MyArtitleViewHolder(itemView: View) : ViewHolder(itemView) {
        val article_img: ImageView = itemView.findViewById(R.id.article_imgg)
        val article_lexile: TextView = itemView.findViewById(R.id.article_lexile)
        val article_type: TextView = itemView.findViewById(R.id.article_type)
        val article_title: TextView = itemView.findViewById(R.id.article_title)
        val article_word: TextView = itemView.findViewById(R.id.article_wordNum)
    }
}